package com.vancouver.knows.service;

import android.app.IntentService;
import android.content.Intent;
import android.os.Message;
import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.util.PhoConstants;

public class RegistrationInstanceIdService extends IntentService {

	private static final String TAG = "RegIntentService";

	public RegistrationInstanceIdService() {
		super(TAG);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		try {
			//InstanceID instanceID = InstanceID.getInstance(this);
			//String token = instanceID.getToken(CommonUtilities.SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
			//Log.i(TAG, "GCM Registration Token: " + token);
			// Implement this method to send any registration to your

			String refreshedToken = FirebaseInstanceId.getInstance().getToken();
			Log.i(TAG, "Token: " + refreshedToken);

			// app's servers.
			sendRegistrationToServer(refreshedToken);

		} catch (Exception e) {
			Log.d(TAG, "Failed to complete token refresh", e);
		}
	}

	private void sendRegistrationToServer(String token) {
		Message msg = Message.obtain();
		MainApplication imContext = (MainApplication) getApplication();
		msg.what = PhoConstants.FCM_STATUS_TOKEN;
		msg.obj = token;
		imContext.handleMobMessage(msg);
	}

}
